设计模式-创建型模式-工厂模式(工厂三兄弟)
工厂模式分为简单工厂,工厂方法,抽象工厂。
简单工厂模式
一个接口,三个具体类,一个工厂,通过选择,生产出对应的对象。
package demo2;
public class Circle implements Shape{
public Circle() {
}
@Override
public void draw() {
}
}
package demo2;
public class Rectangle implements Shape{
public Rectangle() {
}
@Override
public void draw() {
}
}
package demo2;
public interface Shape {
void draw();
}
package demo2;
public class ShapeFactory {
public static Shape getShape(String shapeType) {
if(shapeType == null) {
return null;
}
if (shapeType == "Circle") {
return new Circle();
}
if (shapeType == "Rectangle") {
return new Rectangle();
}
if (shapeType == "Square") {
return new Square();
}
return null;
}
}
package demo2;
public class Square implements Shape{
public Square() {
}
@Override
public void draw() {
}
}
package demo2;
public class Test {
public static void main(String[] agrs) {
Shape circle = new ShapeFactory().getShape("Circle");
circle.draw();
}
}
利用反射改进工厂模式
由于违反开放-封闭原则,每次都要去修改源代码,所以直接使用反射机制。
将类传入,使用泛型+反射,生产出对象。
package demo2;
public class ShapeFactory2 {
@SuppressWarnings("deprecation")
public static Object getClass(Class<? extends Shape> clazz) { // 代表通配的是Shape的子类,即都为借口的子类
Object obj = null;
try {
obj = Class.forName(clazz.getName()).newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return obj;
}
}
package demo2;
public class Test {
public static void main(String[] agrs) {
Shape circle = (Circle)ShapeFactory2.getClass(Circle.class);
circle.draw();
}
}
工厂方法
每个对象有对应的工厂,将原先的工厂拆分。
package demo2;
public class CircleFactory implements Factory{
@Override
public Shape getShape() {
return new Circle();
}
}
package demo2;
public class RectangleFactory implements Factory{
@Override
public Shape getShape() {
return new Rectangle();
}
}
package demo2;
public class SquareFactory implements Factory{
@Override
public Shape getShape() {
return new Square();
}
}
package demo2;
public class Test {
public static void main(String[] agrs) {
Factory circleFactory = new CircleFactory();
Shape circle = circleFactory.getShape();
circle.draw();
}
}
抽象工厂
两个抽象接口,一个为产品,一个为工厂,进行生产
产品
枪
public interface Gun{
public void shooting();
}
子弹
public interface Bullet{
public void load();
}
ak类
public class AK implements Gun{
@Override
public void shooting(){};
}
对应子弹
public class AK_Bullet implements Bullet{
@Override
public void load(){};
}
m4
public class M4 implements Gun{
@Override
public void shooting(){};
}
对应子弹
public class M4_Bullet implements Bullet{
@Override
public void load(){};
}
工厂
public interface Factory{
public Gun produceGun();
public Bullet produceBullet();
}
具体工厂
ak
public class AK_Factory implements Factory {
@Override
public Gun produceGun(){
return new AK();
}
@Override
public Bullet produceBullet(){
return new AK_Bullete();
}
}
M4
public class M4_Factory implements Factory{
@Override
public Gun produceGun(){
return new M4();
}
@Override
public Bullet produceBullet(){
return new M4_Bullet();
}
}
www.iming.info
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。